﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Foothill.Common;
using Foothill.Model;
using Foothill.Service.Session;

namespace Foothill.Web
{
    public partial class ForSaleList : FoothillBasePage
    {
        // filter the search result set after a filter checkbox is checked/unchecked
        private void FilterAdList(ForSaleAdAdvancedSearchParameter searchParam)
        {
            object obj = _sessionService.GetSessionValue(SessionNames.CompleteSearchResultList);

            IList<ForSaleAd> completeList = obj as IList<ForSaleAd>;

            if (completeList == null)
            {
                return;
            }

            // filter the list and get the first page
            IList<ForSaleAd> filteredList = _adSearchService.FilterSearchResultList(searchParam, completeList);

            // add the filtered list to the session, this will be used when paging
            _sessionService.AddSessionValue(SessionNames.DisplaySearchResultList, filteredList);

            gvwAdList.DataSource = filteredList;
            gvwAdList.DataBind();

            CurrentDisplayListSize = filteredList.Count;
        }

        protected void cbxMake_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox senderCheckBox = sender as CheckBox;
            int numOfCheckedCheckBox = GetNumOfCheckedCheckBox(gvwGroupByMakes);
            if (numOfCheckedCheckBox == 0)
            {
                // a special case when all the checkboxes are unchecked, make all the makes selected
                ForSaleAdAdvancedSearchParameter searchParam = GetSearchParameterFromSession();

                foreach (var item in searchParam.SelectedMakeAndModelIdLookup)
                {
                    item.Selected = true;
                }

                FilterAdList(searchParam);
                _sessionService.SaveSessionValue(SessionNames.ForSaleAdvancedSearchParam, searchParam);
            }
            else if (numOfCheckedCheckBox == 1 && senderCheckBox.Checked)
            {
                // a special case when only 1 checkbox is checked and it is the sender, clear all the selected make
                // this is needed b/c we have the special case above
                ForSaleAdAdvancedSearchParameter searchParam = GetSearchParameterFromSession();

                foreach (var item in searchParam.SelectedMakeAndModelIdLookup)
                {
                    item.Selected = false;
                }

                _sessionService.SaveSessionValue(SessionNames.ForSaleAdvancedSearchParam, searchParam);
                UpdateAdListAfterCategorySelectChanged(AdListCategory.Make, sender as CheckBox);
            }
            else
            {
                UpdateAdListAfterCategorySelectChanged(AdListCategory.Make, sender as CheckBox);
            }
        }

        protected void cbxListingType_CheckedChanged(object sender, EventArgs e)
        {
            UpdateAdListAfterCategorySelectChanged(AdListCategory.ListingType, sender as CheckBox);
        }

        protected void cbxCity_CheckedChanged(object sender, EventArgs e)
        {
            UpdateAdListAfterCategorySelectChanged(AdListCategory.City, sender as CheckBox);
        }

        protected void cbxCondition_CheckedChanged(object sender, EventArgs e)
        {
            UpdateAdListAfterCategorySelectChanged(AdListCategory.Condition, sender as CheckBox);
        }

        protected void cbxTransmissionType_CheckedChanged(object sender, EventArgs e)
        {
            UpdateAdListAfterCategorySelectChanged(AdListCategory.TransmissionType, sender as CheckBox);
        }

        protected void MileageCheckBoxCheckedChanged(object sender, EventArgs e)
        {
            UpdateAdListAfterCategorySelectChanged(AdListCategory.Mileage, sender as CheckBox);
        }

        protected void YearAgeCheckBoxCheckedChanged(object sender, EventArgs e)
        {
            UpdateAdListAfterCategorySelectChanged(AdListCategory.YearAge, sender as CheckBox);
        }

        private void UpdateAdListAfterCategorySelectChanged(AdListCategory adListCategory, CheckBox cbxChanged)
        {
            GridViewRow row = (GridViewRow)cbxChanged.NamingContainer;
            GridView gridView = GetGridViewForAdListCategory(adListCategory);

            int key = (int)gridView.DataKeys[row.RowIndex].Value;

            AdCategorySelectItem newItem = new AdCategorySelectItem
            {
                Action = cbxChanged.Checked ? SelectItemAction.Add : SelectItemAction.Remove,
                Category = adListCategory,
                SelectedIdValue = key,
                SelectedKeyName = cbxChanged.Text
            };

            UpdateAdListAfterCategorySelectChanged(newItem);
        }

        private void UpdateAdListAfterCategorySelectChanged(AdCategorySelectItem newItem)
        {
            ForSaleAdAdvancedSearchParameter searchParam = GetSearchParameterFromSession();

            if (searchParam != null)
            {
                searchParam.AddFilterItem(newItem);

                FilterAdList(searchParam);

                // save the select items list
                _sessionService.SaveSessionValue(SessionNames.ForSaleAdvancedSearchParam, searchParam);
            }
        }
    }
}